package com.bit.transaction.controller;

import com.bit.transaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //jdbc事务管理器
    @Autowired
    private DataSourceTransactionManager transactionManager;
    //定义事务属性
    @Autowired
    private TransactionDefinition transactionDefinition;

    @RequestMapping("/registry")
    public String registry(String name, String password){
        //开启事务
        TransactionStatus status = transactionManager.getTransaction(transactionDefinition);
        userService.registryUser(name, password);
//        //提交事务
//        transactionManager.commit(status);
        //回滚事务
        transactionManager.rollback(status);
        return "注册用户成功";
    }

}
